<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="./css/pintuer.css">
    <link rel="stylesheet" href="./css/admin.css">
    <script src="./js/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="./js/pintuer.js"></script>
</head>
<body>
<div class="panel admin-panel">
    <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>车辆修改</strong></div>
    <div class="body-content">
        <form method="post" class="form-x" action="">

        </form>
    </div>
</div>


<script type="text/javascript">
    $(function(){


        var bus_id=localStorage.getItem("bus_id");
        console.log(bus_id);
        //客车渲染
        $.ajax({
            type:"POST",
            url:"/busIdlist",
            data:{"bus_id":bus_id},

            datatype:'json',
            success:function(data){
                console.log(data);
                if(data.status==401){
                    alert("熔断开启，系统异常");
                    window.location.href="login.html";
                    return;


                }else if(data.status==200) {

                    var lists = data.data;
                    for (var i = 0; i < lists.length; i++) {
                        var html = "";
                        html += `

                               <div class="form-group">
                <div class="label">
                    <label>车辆名字：</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" value="${lists[i].bus_name}" id="bus_name" name="bus_name" data-validate="required:请输入标题" />
                    <div class="tips"></div>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>车牌号：</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" value="${lists[i].bus_s}" id="bus_s" name="bus_s" data-validate="required:请输入标题" />
                    <div class="tips"></div>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>可乘坐人数：</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" value="${lists[i].bus_seat}" id="bus_seat" name="bus_seat" data-validate="required:请输入标题" />
                    <div class="tips"></div>
                </div>
            </div>

            <div class="form-group">
                <div class="label">
                    <label>基本价格/天：</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" value="${lists[i].bus_price}" id="bus_price" name="bus_price" data-validate="required:请输入标题" />
                    <div class="tips"></div>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 100px;">
                <div class="label">
                    <label>车辆图片：</label>
                </div>
                <div class="field">
                    <input type="file" name="bus_photo" id="bus_photo"
                           accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" onchange="showimgs()"
                            value="${lists[i].bus_photo}"
                           style="opacity:0;position: absolute;left: 10px;width: 100px;height: 40px;">
                    <input type="button" class="button bg-blue margin-left" id="image13" value="+ 浏览上传"  style="float:left;">
                    <div class="tipss">图片尺寸：500*500</div>
                </div>
                <div id="shows" style="width: 10px;height: 10px;">
                          <img src="/imgshows?picName=${lists[i].bus_photo}" style="width: 100px;height: 100px;margin-left:160px;margin-top:5px;">
                </div>
            </div>

            <div class="form-group" style="margin-bottom: 100px;">
                <div class="label">
                    <label>年检照片：</label>
                </div>
                <div class="field">
                    <input type="file" name="bus_year" id="bus_year"
                           accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" onchange="showimg()"
                           value="${lists[i].bus_year}"
                           style="opacity:0;position: absolute;left: 10px;width: 100px;height: 40px;">
                    <input type="button" class="button bg-blue margin-left" id="image1" value="+ 浏览上传"  style="float:left;">
                    <div class="tipss">图片尺寸：500*500</div>
                </div>
                <div id="show" style="width: 10px;height: 10px;">
                     <img src="/imgshows?picName=${lists[i].bus_year}" style="width: 100px;height: 100px;margin-left:160px;margin-top:5px;">
                </div>
            </div>


            <div class="form-group">
                <div class="label">
                    <label>描述：</label>
                </div>
                <div class="field">
                    <textarea class="input" value="${lists[i].bus_remarks}" name="note" id="bus_remarks" style=" height:90px;"></textarea>
                    <div class="tips"></div>
                </div>
            </div>




            <div class="form-group">
                <div class="label">
                    <label></label>
                </div>
                <div class="field">
                    <input type="button" onclick="updatebus()" class="button bg-blue margin-left" value="修改">
                    <input type="button" onclick="deletebus()" class="button bg-blue margin-left" style="margin-left: 100px;" value="删除车辆">
                </div>



            </div>



                    `

                        $('.form-x').append(html);

                    }

                }else if(data.status==500){
                      alert("查看失败");
                      return;
                }


            }
        });


    })


</script>

<script type="text/javascript">
    function updatebus() {
        var bus_id=localStorage.getItem("bus_id");
        console.log(bus_id);
        var fordata = new FormData();
        fordata.append('bus_name', $('#bus_name').val());
        fordata.append('bus_s', $('#bus_s').val());
        fordata.append('bus_id', bus_id);
        fordata.append('bus_seat', $('#bus_seat').val());
        fordata.append('bus_price', $('#bus_price').val());
        fordata.append('bus_remarks', $('#bus_remarks').val());
        fordata.append('bus_photo', $('#bus_photo')[0].files[0]);
        fordata.append('bus_year', $('#bus_year')[0].files[0]);
        console.log(fordata);
        $.ajax({
            type: "post",
            url: "/updatabus",
            processData: false,
            contentType: false,
            cache: false,
            async: false,
            // contentType: 'application/json; charset=UTF-8',
            // dataType:'json',
            data: fordata,
            success: function (data) {
                console.log(data);
                if (data.status == 401) {
                    alert("熔断开启，系统异常");
                    window.location.href = "login.html";
                    return;
                }
                if (data.status == 200) {
                    alert("修改成功");
                    window.location.href = "index.html";

                    return;
                }
                if (data.status == 500) {
                    alert("修改错误");
                    window.location.href = "updatebus.html";
                    return;
                }
                if (data.status == null) {
                    alert("修改异常");
                    window.location.href = "updatebus.html";
                    return;
                }
                if (data.status == 20) {

                    return;
                }
            }
        });
        return false;

    }

    function deletebus() {
        var bus_id=localStorage.getItem("bus_id");
        $.ajax({
            type:"POST",
            url: "/deletebus",
            data:({
                bus_id:bus_id
            }),
            success: function (data) {
                console.log(data);
                if(data.status==200){
                    alert("删除成功");
                    window.location.href = "index.html";
                    return;
                }
                if(data.status==500){
                    alert("删除失败");
                    window.location.href = "index.html";
                    return;
                }
            }
        })

    }

</script>


<script type="text/javascript">
    function showimgs() {
        var filePath= $("#bus_photo").val();
        if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(filePath)){
            alert('"提示", "请上传图片（格式BMP、JPG、JPEG、PNG、GIF等）!"');
        }else{

            var url = null;
            var fileimg = document.getElementById("bus_photo").files[0];
            if(window.createObjectURL != undefined){
                url = window.createOjcectURL(fileimg);
            }
            else if(window.URL != undefined){
                url = window.URL.createObjectURL(fileimg);
            }else if(window.webkitURL != undefined){
                url = window.webkitURL.createObjectURL(fileimg)
            }
            var html ="";
            html+='<img src="'+url+'" style="width: 100px;height: 100px;margin-left:160px;margin-top:5px;"/>';
            $("#shows").html(html);
        }
    }

    function showimg() {
        var filePath= $("#bus_year").val();
        if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(filePath)){
            alert('"提示", "请上传图片（格式BMP、JPG、JPEG、PNG、GIF等）!"');
        }else{

            var url = null;
            var fileimg = document.getElementById("bus_year").files[0];
            if(window.createObjectURL != undefined){
                url = window.createOjcectURL(fileimg);
            }
            else if(window.URL != undefined){
                url = window.URL.createObjectURL(fileimg);
            }else if(window.webkitURL != undefined){
                url = window.webkitURL.createObjectURL(fileimg)
            }
            var html ="";
            html+='<img src="'+url+'" style="width: 100px;height: 100px;margin-left:160px;margin-top:5px;"/>';
            $("#show").html(html);
        }
    }
</script>

</body></html>